package es;

import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/9/1.
 */
public class Tester {

    private Client client;

    public Tester() {
        //使用本机做为节点
        this("127.0.0.1");
    }

    public Tester(String ipAddress) {
        //集群连接超时设置

        Settings settings = ImmutableSettings.settingsBuilder()
                .put("client.transport.sniff", true)
                .put("cluster.name", "manson2")
                .put("node.name", "ThisIsNewShit2")
                .build();
        client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
    }


    /**
     * 建立索引,索引建立好之后,会在elasticsearch-0.20.6\data\elasticsearch\nodes\0创建所以你看
     *
     * @param jsondata json格式的数据集
     * @return
     */
    public void createIndexResponse(String indexname, String type, List<String> jsondata) {
        //创建索引库 需要注意的是.setRefresh(true)这里一定要设置,否则第一次建立索引查找不到数据
        IndexRequestBuilder requestBuilder = client.prepareIndex(indexname, type)
                .setRefresh(true).setCreate(true);
        for (int i = 0; i < jsondata.size(); i++) {
            requestBuilder.setSource(jsondata.get(i)).execute().actionGet();
        }

    }

    /**
     * 创建索引
     *
     * @param jsondata
     * @return
     */
    public IndexResponse createIndexResponse(String indexname, String type, String jsondata) {
        IndexResponse response = client.prepareIndex(indexname, type)
                .setSource(jsondata)
                .execute()
                .actionGet();
        return response;
    }

    /**
     * 执行搜索
     *
     * @param queryBuilder
     * @param indexname
     * @param type
     * @return
     */
    public List<News> searcher(QueryBuilder queryBuilder, String indexname, String type) {
        List<News> list = new ArrayList<News>();
        SearchResponse searchResponse = client.prepareSearch(indexname).setTypes(type)
                .setQuery(queryBuilder)
                .execute()
                .actionGet();
        SearchHits hits = searchResponse.getHits();
        System.out.println("查询到记录数=" + hits.getTotalHits());
        SearchHit[] searchHists = hits.getHits();
        if (searchHists.length > 0) {
            for (SearchHit hit : searchHists) {
                String data = hit.getSourceAsString();
                System.out.println("Result:" + data);
            }
        }
        return list;
    }


    public static void main(String[] args) {
        Tester esHandler = new Tester();
        List<String> jsondata = DataFactory.getInitJsonData();
        String indexname = "bank";
        String type = "account";
        esHandler.createIndexResponse(indexname, type, jsondata);
        //查询条件
        QueryBuilder queryBuilder = QueryBuilders.queryStringQuery("Skinserve");
        esHandler.searcher(queryBuilder, indexname, type);
    }

}
